﻿// --------------------------------
// <copyright file="GenerateCalculationModuleForm.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using Tronsoft.Actucal.CalculationEngineCreator;

namespace Actucal
{
    public partial class GenerateCalculationModuleForm : Form
    {
        public GenerateCalculationModuleForm()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("Geef een opslaglocatie op.", "Fout opslaglocatie");
                return;
            }

            Product product = ProductsService.CurrentProduct;
            if (product != null)
            {
                // the model
                CalculationModel model = new CalculationModel();
                // Get the constants
                //if (product.constants != null)
                //{
                model.Constants.AddRange(product.Constants);
                //}

                // Get the calculations
                foreach (KeyValuePair<string, string> pair in product.Attributes)
                {
                    model.AddCalculation(pair.Key, pair.Value);
                }

                // Get the parameters
                model.CalculationParameters.AddRange(product.GetParameters());

                // Delete file if it exists
                File.Delete(txtFilePath.Text);
                try
                {
                    model.SaveAssembly(txtFilePath.Text);

                    // Show that the data was saved
                    MessageBox.Show("De rekenmodule is bewaar.", "Opslag rekenmodule");
                }
                catch (CalculationException ex)
                {
                    txtErrors.Text = ex.ToString();

                    // Show that the data was saved
                    MessageBox.Show("De rekenmodule is niet bewaard.", "Opslag fout rekenmodule");
                }
            }
            else
            {
                MessageBox.Show("Kies een product om een module voor te genereren.", "Fout generatie rekenmodule");
                return;
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == saveFileDialog.ShowDialog(this))
            {
                txtFilePath.Text = saveFileDialog.FileName;
            }
        }
    }
}